package main.rabbitmqProvider.config;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfirmConfig implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);

        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returned) {
                System.out.println("消息发送失败,进行后续处理");
                System.out.println("消息内容: " + new String(returned.getMessage().getBody()));
                System.out.println("交换机: " + returned.getExchange());
                System.out.println("路由键: " + returned.getRoutingKey());
                System.out.println("错误码: " + returned.getReplyCode());
                System.out.println("错误信息: " + returned.getReplyText());
            }
        });
    }
}
